import 'package:flame/game.dart';
import 'package:flutter/material.dart';


class LevelSelectionScreen extends StatelessWidget {
  final Function(int level) onLevelSelected;
  final Function() onBackToHome; // 添加返回首页的回调

  const LevelSelectionScreen({super.key, required this.onLevelSelected, required this.onBackToHome});

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: () async {
        // 调用返回首页的回调
        onBackToHome();
        return false;
      },
      child: Scaffold(
        appBar: AppBar(
          title: const Text('选择关卡'),
          backgroundColor: const Color(0xFF2196F3),
          foregroundColor: Colors.white,
          leading: IconButton(
            icon: const Icon(Icons.arrow_back),
            onPressed: () => onBackToHome(), // 添加 AppBar 返回按钮
          ),
        ),
        body: Container(
          color: const Color(0xFF151515), // 深灰色背景
          child: GridView.builder(
            padding: const EdgeInsets.all(20),
            gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
              crossAxisCount: 3,
              crossAxisSpacing: 20,
              mainAxisSpacing: 20,
            ),
            itemCount: 12, // 12个关卡
            itemBuilder: (context, index) {
              final level = index + 1;
              return ElevatedButton(
                onPressed: () => onLevelSelected(level),
                style: ElevatedButton.styleFrom(
                  backgroundColor: const Color(0xFF2196F3),
                  foregroundColor: Colors.white,
                  padding: const EdgeInsets.all(20),
                  shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.circular(10),
                  ),
                ),
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    Text(
                      '关卡 $level',
                      style: const TextStyle(
                        fontSize: 18,
                        fontWeight: FontWeight.bold,
                      ),
                    ),
                    const SizedBox(height: 10),
                    Icon(
                      level <= 3
                          ? Icons.star
                          : level <= 6
                              ? Icons.star_half
                              : Icons.star_border,
                      size: 30,
                      color: Colors.yellow,
                    ),
                  ],
                ),
              );
            },
          ),
        ),
      ),
    );
  }
}